package top.lyc829.zzl_android.ui.profile.user_dtc.dynamic;

import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xuexiang.xutil.data.DateUtils;

import org.jetbrains.annotations.NotNull;

import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.util.DynamicType;
import top.lyc829.zzl_android.logic.util.RichUtils;
import top.lyc829.zzl_android.logic.util.Utils;

public class   UserDynamicAdapter extends BaseQuickAdapter<DynamicBean, BaseViewHolder>implements LoadMoreModule {

    public UserDynamicAdapter(){super(R.layout.my_dynamic_new); }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, DynamicBean dynamicBean) {
        baseViewHolder.setText(R.id.dynamic_text,dynamicBean.getDynamicText());
        baseViewHolder.setText(R.id.my_dynamic_user_name,dynamicBean.getUserName());
        baseViewHolder.setText(R.id.dynamic_item_create_time, DateUtils.millis2String(Long.parseLong(dynamicBean.getCreateTime()), DateUtils.yyyyMMdd.get()));

        if (Utils.isStringEmpty(dynamicBean.getDynamicUrl().get(0))) {
            Glide.with(MyApp.getContext())
                    .load(R.drawable.travels_item_placeholder)//load放地址
                    .into((ImageView) baseViewHolder.getView(R.id.dynamic_ngl_images));
            ((ImageView) baseViewHolder.getView(R.id.dynamic_ngl_images)).setVisibility(View.GONE);
        } else {
            ((ImageView) baseViewHolder.getView(R.id.dynamic_ngl_images)).setVisibility(View.VISIBLE);
//            ImageView imageView = ((ImageView) baseViewHolder.getView(R.id.dynamic_ngl_images));
//            String avatarTag = (String) imageView.getTag();
//            //如果有图片的TAG有并且和之前设置的一样，那么直接再次加载即可
//            if (null == avatarTag || avatarTag.equals(dynamicBean.getDynamicUrl().get(0))) {
//                Glide.with(imageView)
//                        .load(dynamicBean.getDynamicUrl().get(0))
//                        .into(imageView);
//            }  //如果和原来的tag不同，也就是url不同，那么就先清空原来的图片，然后加载新的图片
//            else {
//                imageView.setImageDrawable(ContextCompat.getDrawable(MyApp.getContext(), R.drawable.travels_item_placeholder));
//                Glide.with(imageView)
//                        .load(dynamicBean.getDynamicUrl().get(0))
//                        .into(imageView);
//            }

            Glide.with(MyApp.getContext())
                    .load(dynamicBean.getDynamicUrl().get(0))//load放地址
                    .into((ImageView) baseViewHolder.getView(R.id.dynamic_ngl_images));

        }
        Glide.with(MyApp.getContext())
                .load(dynamicBean.getUserUrl())
                .into((ImageView) baseViewHolder.getView(R.id.my_dynamic_user_Url));

        // 转发处理
        if (!dynamicBean.getType().equals(DynamicType.ORIGINAL_DYNAMIC)) {

            baseViewHolder.getView(R.id.dynamic_ngl_images).setVisibility(View.GONE);
            baseViewHolder.getView(R.id.edit_share).setVisibility(View.VISIBLE);
            if (!dynamicBean.getType().equals(DynamicType.TRAVEL)) {
                Glide.with(MyApp.getContext())
                        .load(dynamicBean.getShareDynamic().getImg())
                        .into((ImageView)baseViewHolder.getView(R.id.original_avatar_dynamic));
            } else {
                Glide.with(MyApp.getContext())
                        .load(dynamicBean.getShareDynamic().getImg())
                        .into((ImageView)baseViewHolder.getView(R.id.original_avatar_dynamic));
            }

            baseViewHolder.setText(R.id.original_user_name_dynamic, dynamicBean.getShareDynamic().getTitle());
            baseViewHolder.setText(R.id.original_text_dynamic, dynamicBean.getShareDynamic().getText());
        } else {
            if (Utils.isStringEmpty(dynamicBean.getDynamicUrl().get(0))) {
                baseViewHolder.getView(R.id.dynamic_ngl_images).setVisibility(View.GONE);
            } else {
                baseViewHolder.getView(R.id.dynamic_ngl_images).setVisibility(View.VISIBLE);
            }
            baseViewHolder.getView(R.id.edit_share).setVisibility(View.GONE);
        }

    }
}
